package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Examination;
import cn.wolfcode.crm.mapper.ExaminationMapper;
import cn.wolfcode.crm.mapper.PotentialCustomerMapper;
import cn.wolfcode.crm.query.QueryForObject;
import cn.wolfcode.crm.service.IExaminationService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExaminationServiceImpl implements IExaminationService {

    @Autowired
    private ExaminationMapper mapper;
    @Autowired
    private PotentialCustomerMapper potentialCustomerMapper;

    public void deleteByPrimaryKey(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void insert(Examination record) {
//      得到当前用户的对象
        Employee emp = (Employee) SecurityUtils.getSubject().getPrincipal();
        if(emp != null)
        {
            record.setInputUser(emp);
        }
        mapper.insert(record);
        //保存可能更改的潜在学员意向班级----周露
        potentialCustomerMapper.updateClassByExamination(record.getPotentialCustomer());

    }

    public Examination selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<Examination> selectAll() {
        return mapper.selectAll();
    }

    public void updateByPrimaryKey(Examination record) {
        mapper.updateByPrimaryKey(record);
    }

    public PageResult query(QueryForObject qo) {
        int total = mapper.queryForTotal(qo);
        if (total == 0) {
            return new PageResult();
        }

        List<Examination> rows = mapper.queryForList(qo);
        return new PageResult(total, rows);
    }

//    审核操作
    public void changeState(Examination record) {
//        Examination record = mapper.selectByPrimaryKey(id);
        Employee emp = (Employee) SecurityUtils.getSubject().getPrincipal();
        if(emp != null)
        {
            record.setHandler(emp);
        }
        mapper.check(record);
    }
}
